package com.lovepick.http;

import java.util.List;

import org.apache.http.NameValuePair;

import com.lovepick.util.Constant;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


public class HttpThread extends Thread
{
	private Handler mHandler;
	private HttpHelper mHttp;
	private List<NameValuePair> mParamsList;
	private String mUrl;
	private int mMethod;

	public HttpThread(Handler handler, int method, String url,
			List<NameValuePair> paramsList)
	{
		this.mHandler = handler;
		this.mMethod = method;
		this.mUrl = url;
		this.mParamsList = paramsList;
	}

	@Override
	public void run()
	{
		mHttp = new HttpHelper();
		String result = null;

		// get
		if (Constant.HTTP_METHOD_GET == this.mMethod)
		{
			result = mHttp.doGet(mUrl);
		}
		// post
		else if (Constant.HTTP_METHOD_POST == this.mMethod)
		{
			result = mHttp.doPost(mUrl, mParamsList);
		}

		if (Constant.HTTP_ERROR.equals(result))
		{
			Message msg = new Message();
			msg.what = Constant.MSG_HTTP_ERROR;
			mHandler.sendMessage(msg);
		}
		else
		{
			Message msg = new Message();
			msg.what = Constant.MSG_HTTP_SUCCESS;
			Bundle bundle = new Bundle();
			bundle.putString(Constant.HTTP_RESULT, result);
			msg.setData(bundle);
			mHandler.sendMessage(msg);
		}
	}
}
